package com.example.springapplicationdemo.exceptionhandler;

import com.example.springapplicationdemo.exceptionhandler.exception.UserDisabledException;
import com.example.springapplicationdemo.exceptionhandler.exception.UserException;
import com.example.springapplicationdemo.exceptionhandler.exception.UserNotFoundException;
import com.example.springapplicationdemo.vo.RestResVo;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("user_rest_controller")
public class UserRestController {

    /**
     * 正常处理流程
     * @param id
     * @return
     */
    @PostMapping("{id}/_update")
    public RestResVo<Void> updateUser(@PathVariable Long id){
        if (id.equals(1L)){
            throw new UserNotFoundException(id);
        }
        if (id.equals(2L)){
            throw new UserDisabledException(id);
        }
        return RestResVo.success(null);
    }


    /**
     * 异常处理流程<br />
     * Controller中的ExceptionHandler只会处理本Controller抛出的异常 <br />
     * 指定处理的异常UserNotFoundException
     * @param e
     * @return
     */
    @ExceptionHandler(value = {UserNotFoundException.class})
    public RestResVo<Void> handleException(UserException e){
        return RestResVo.error(e);
    }

}
